<template>
  <div class="tabelContainer">
    <basic-container>
      <avue-crud :option="option"
                 :table-loading="loading"
                 :data="data"
                 :page="page"
                 ref="crud"
                 @row-del="rowDel"
                 v-model="form"
                 :permission="permissionList"
                 @row-update="rowUpdate"
                 @row-save="rowSave"
                 :before-open="beforeOpen"
                 @search-change="searchChange"
                 @search-reset="searchReset"
                 @selection-change="selectionChange"
                 @current-change="currentChange"
                 @size-change="sizeChange"
                 @refresh-change="refreshChange"
                 @on-load="onLoad">
        <template slot="menuLeft">
          <el-button type="danger"
                     size="small"
                     icon="el-icon-delete"
                     plain
                     v-if="permission.noticeList_delete"
                     @click="handleDelete">删 除
          </el-button>
        </template>
        <!--   自定义发布时间 显示时分秒-->
        <template slot="releaseTime" slot-scope="scope">
          <div>{{ scope.row.releaseTime }}</div>
        </template>

        <template slot-scope="{row}"
                  slot="category">
          <el-tag>{{ row.categoryName }}</el-tag>
        </template>
        //自定义查看
        <template slot-scope="scope" slot="menu">
          <el-button icon="el-icon-view" size="small" type="text" @click="see(scope.row)" v-if="permissionList.viewBtn">
            查看
          </el-button>
        </template>
      </avue-crud>
      <el-dialog title="查看"
                 :visible.sync="box"
                 append-to-body
                 width="60%">
        <el-row>
          <div class="title">{{ this.rowView.title }}</div>
          <div class="text">
            <div class="time">发布时间：{{ this.rowView.releaseTime }}</div>
            <div class="person">发布人：{{ this.rowView.releasePerson }}</div>
          </div>
          <div class="content" v-html="this.rowView.content"></div>

        </el-row>
        <!--      <span slot="footer" class="dialog-footer">-->
        <!--        <el-button @click="box=false">取 消</el-button>-->
        <!--        <el-button type="primary" @click="box=false">确 定</el-button>-->
        <!--     </span>-->
      </el-dialog>
    </basic-container>
  </div>
</template>

<script>
import {getList, remove, update, add, getNotice} from "@/api/dust/noticeList";
import {mapGetters} from "vuex";

export default {
  data() {
    return {
      search: {},
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: this.$pageSize,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        // height: 'auto',//20201103 高度自适应
        calcHeight: 30,
        dialogWidth: 950,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        index: true,
        viewBtn: false,//查看按钮
        selection: true,
        // excelBtn: true,//导出
        dialogClickModal: false,

        // column: [
        //   {
        //     label: "标题",
        //     prop: "title",
        //     span: 24,
        //     row: true,
        //     search: true,
        //     rules: [{
        //       required: true,
        //       message: "请输入标题",
        //       trigger: "blur"
        //     }],
        //   },
        //   {
        //     label: "发布时间",
        //     prop: "releaseTime",
        //     span: 20,
        //     type: "date",
        //     format: "yyyy-MM-dd hh:mm:ss",
        //     valueFormat: "yyyy-MM-dd hh:mm:ss",
        //     pickerOptions: {
        //       disabledDate(time) {
        //         return time.getTime() > Date.now();
        //       },
        //     },
        //     slot: true,//自定义列 搜索
        //     rules: [{
        //       required: true,
        //       message: "请输入发布时间",
        //       trigger: "click"
        //     }],
        //     // searchslot: true,//自定义搜索
        //     // search: true,
        //     // searchRange:true,
        //     search: true,
        //     // formslot: true,
        //     // labelslot: true,
        //     // errorslot: true,
        //   },
        //   {
        //     label: "正文",
        //     prop: "content",
        //     component: 'AvueUeditor',
        //     options: {
        //       action: '/api/blade-resource/oss/endpoint/put-file',//图片上传地址
        //       props: {
        //         res: "data",//返回的数据结构层次
        //         url: "link",//返回结构体图片地址字段
        //       }
        //     },
        //     hide: true,
        //     minRows: 6,
        //     span: 24,
        //   }
        // ],
        column: [
          {
            label: "标题",
            prop: "title",
            align: 'center',
            display: false,
            search: true,
            searchLabelWidth:48
          },
          {
            label: "发布时间",
            prop: "releaseTime",
            align: 'center',
            display: false,
            type: "date",
            format: "yyyy-MM-dd",
            valueFormat: "yyyy-MM-dd",
            search: true,
            slot: true,//自定义列 发布时间显示时分秒
          }
        ],
        group: [
          {
            label: '主要内容',
            prop: 'group1',
            column: [
              {
                label: "标题",
                prop: "title",
                rules: [{
                  required: true,
                  message: "请输入标题",
                  trigger: "blur"
                }]
              },
              {
                label: '发布时间',
                prop: 'releaseTime',
                type: "datetime",
                format: "yyyy-MM-dd HH:mm:ss",
                valueFormat: "yyyy-MM-dd HH:mm:ss",
                // pickerOptions: {
                //   disabledDate(time) {
                //     return time.getTime() > Date.now();//不能大于当前时间
                //   },
                // },
                slot: true,//自定义列 搜索
                rules: [{
                  required: true,
                  message: "请输入发布时间",
                  trigger: "click"
                }],
              },
              {
                label: "发布人",
                prop: "releasePerson",
                rules: [{
                  required: true,
                  message: "请输入发布人",
                  trigger: "blur"
                }]
              },
              {
                label: "正文",
                prop: "content",
                component: 'AvueUeditor',
                // customConfig: {
                //   multipartFile: 'multipartFile'
                // },
                params: {
                  options: {
                    action: '/api/squirrel-ficm/file/upload',//图片上传地址
                    props: {
                      res: "data",//返回的数据结构层次
                      url: "",//返回结构体图片地址字段
                    }
                  },
                },
                hide: true,
                minRows: 6,
                span: 24,
              },
            ]
          }
        ]
      },
      data: [],
      dialogVisible: false,
      box: false,
      rowView: '',
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.noticeList_add, false),
        viewBtn: this.vaildData(this.permission.noticeList_view, false),
        delBtn: this.vaildData(this.permission.noticeList_delete, false),
        editBtn: this.vaildData(this.permission.noticeList_edit, false)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },
  mounted() {
  },
  methods: {
    see(row) {
      this.box = true;
      console.log('view row', row)
      this.rowView = row
    },
    rowSave(row, done, loading) {
      add(row).then(() => {
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
        done();
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    rowUpdate(row, index, done, loading) {
      update(row).then(() => {
        // console.log('row', this.form)
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
        done();
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    searchSubmit(item) {
      this.onLoad(JSON.stringify(item))
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        getNotice(this.form.id).then(res => {
          this.form = res.data.data;
        });
      }
      done();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    },
  }
};
</script>

<style scoped>
.title {
  text-align: center;
  font-size: 28px;
  font-weight: 600;
}

.text {
  display: flex;
  flex-direction: row;
  justify-content: center;
  margin: 20px -20px;
  padding-bottom: 20px;
  border-bottom: 1px solid #eee;
}

.time {
  margin-right: 40px;
}

.person {
}

.content {
}
</style>
